package com.csi.service.impl;

import com.csi.dao.UserInfoDao;
import com.csi.dao.impl.UserInfoDaoImpl;
import com.csi.domain.UserInfo;
import com.csi.service.UserInfoService;
import com.csi.utils.MD5Utils;

import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;

public class UserInfoServiceImpl implements UserInfoService {
    @Override
    public UserInfo login(String phone , String password) {
        UserInfoDao userInfoDao = new UserInfoDaoImpl ();
        MD5Utils md5Utils = new MD5Utils ();
        String number = null;
        try {
            number = md5Utils.toMD5 ( password );
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace ();
        }
        UserInfo userInfo = null;
        try {
            userInfo = userInfoDao.login ( phone , number );
        } catch (SQLException e) {
            e.printStackTrace ();
        }
        return userInfo;
    }

    @Override
    public int register(UserInfo userInfo) {
        UserInfoDao userInfoDao = new UserInfoDaoImpl ();
        int num = 0;
        try {
            num = userInfoDao.register ( userInfo );
        } catch (SQLException e) {
            e.printStackTrace ();
        }
        return num;
    }

    @Override
    public int update(UserInfo userInfo) {
        UserInfoDao userInfoDao = new UserInfoDaoImpl ();
        int num = 0;
        try {
            num = userInfoDao.update ( userInfo );
        } catch (SQLException e) {
            e.printStackTrace ();
        }
        return num;
    }

    @Override
    public int update(String password , int userId) {
        UserInfoDao userInfoDao = new UserInfoDaoImpl ();
        MD5Utils md5Utils = new MD5Utils ();
        String number = null;
        try {
            number = md5Utils.toMD5 ( password );
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace ();
        }
        int num = 0;
        try {
            num = userInfoDao.update ( number , userId );
        } catch (SQLException e) {
            e.printStackTrace ();
        }
        return num;
    }
}
